﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AudioStudio
{
    /// <summary>
    /// 声音的听者管理器，仅内部使用
    /// </summary>
    public class ListenerManager
    {
        private static bool ListenerChanged = false;

        private static ulong[] Listeners = {AkSoundEngine.AK_INVALID_GAME_OBJECT};

        public static bool SetListener(GameObject gameObj)
        {
            if (gameObj == null)
                return false;
            ulong gameobjectid = AkSoundEngine.GetAkGameObjectID(gameObj);
            if(gameobjectid != Listeners[0])
            {
                ListenerChanged = true;
                Listeners[0] = gameobjectid;
            }
            return true;
        }

        public static void RemoveListener(GameObject gameObj)
        {
            if (gameObj != null)
            {
                ulong gameobjectid = AkSoundEngine.GetAkGameObjectID(gameObj);
                if (gameobjectid != Listeners[0])
                {
                    return;
                }
            }
            ListenerChanged = true;
            Listeners[0] = AkSoundEngine.GetAkGameObjectID(AudioManager.Instance.GlobalSoundObject);
        }

        public static void Refresh()
        {
            if (ListenerChanged)
            {
                ListenerChanged = false;
                AkSoundEngine.SetDefaultListeners(Listeners, 1);
            }
        }
    }
}